package com.alertdroid;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper 
{
	SQLiteDatabase myDb;
	String dbPath="/data/data/com.alertdroid/databases/";
	static String dbName="alertdroid";
	private Context ctx;
	int rowCount;
	public DatabaseHelper(Context ctx){
		super(ctx,dbName,null,1);		
		this.dbName=dbName;
		this.ctx=ctx;
		this.createDataBase();
	}
	
	 public void createDataBase() {		 		 
	    	boolean dbExist = checkDataBase();	 
	    	if(dbExist){
	    		//do nothing - database already exist
	    	}else{	 
	    		//By calling this method and empty database will be created into the default system path
	               //of your application so we are gonna be able to overwrite that database with our database.
	        	this.getReadableDatabase();	 
	        	copyDataBase();
	    	}	 
	 }
	
	private boolean checkDataBase(){		 
    	SQLiteDatabase checkDB = null; 
    	try{    		
    		String myPath = dbPath + dbName;
    		checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
     	}catch(SQLiteException e){ 
    		//database does't exist yet. 
    	} 
    	if(checkDB != null){ 
    		checkDB.close();     	
    	}
 
    	return checkDB != null ? true : false;
    }
	
	public void copyDataBase(){
		try{
			//Open your local db as the input stream
	    	InputStream myInput = this.ctx.getAssets().open(this.dbName);
	 
	    	// Path to the just created empty db
	    	String outFileName = dbPath + this.dbName;
	 
	    	//Open the empty db as the output stream
	    	OutputStream myOutput = new FileOutputStream(outFileName);
	 
	    	//transfer bytes from the inputfile to the outputfile
	    	byte[] buffer = new byte[1024];
	    	int length;
	    	while ((length = myInput.read(buffer))>0){
	    		myOutput.write(buffer, 0, length);
	    	}
	 
	    	//Close the streams
	    	myOutput.flush();
	    	myOutput.close();
	    	myInput.close();			
		}
		catch(Exception e){
			
		}		
	}
	
	public void executeNonQuery(String sql)
	{
		this.myDb.execSQL(sql);	
	}
	
	public Cursor executeQuery(String sql)
	{
		Cursor cur=null;
		cur=this.myDb.rawQuery(sql, null);
		return cur;	
	}
	
	public void openDB()
	{
		myDb=SQLiteDatabase.openDatabase(dbPath+dbName, null, 
				SQLiteDatabase.OPEN_READWRITE);
		
	}
	
	public void closeDb()
	{
		myDb.close();
	}
	
	public int getRowCount(){		
		return this.rowCount;
	}

	/*
	public String[][] readAllStation(){
		String[][] data=null;
		int counter=0;
		Cursor c = myDb.rawQuery("SELECT * FROM station"
				, null);
		this.rowCount=c.getCount();
        data= new String[c.getCount()][4];
		if (c.moveToFirst())
        {
            do {   
            	data[counter][0]=c.getString(0);
            	data[counter][1]=c.getString(1);
            	data[counter][2]=c.getString(2);
            	data[counter][3]=c.getString(3);
            	         	
            	counter++;
            } while (c.moveToNext());
        }
		return data;
	}
	*/
	public String[][] readAllData()
	{
		String[][] data=null;
		int counter=0;
		Cursor c = myDb.rawQuery("SELECT * FROM alertdroid"
				, null);
		this.rowCount=c.getCount();
        data= new String[c.getCount()][8];
		if (c.moveToFirst())
        {
            do {   
            	data[counter][0]=c.getString(0);
            	data[counter][1]=c.getString(1);
            	data[counter][2]=c.getString(2);
            	data[counter][3]=c.getString(3);
            	data[counter][4]=c.getString(4);
            	data[counter][5]=c.getString(5);  
            	data[counter][6]=c.getString(6);
            	data[counter][7]=c.getString(7);
            	counter++;
            } while (c.moveToNext());
        }
		return data;		
	}
	
	public String[] readName()
	{
		String[] data=null;
		int counter=0;
		Cursor c = myDb.rawQuery("SELECT name FROM alertdroid"
				, null);
        data= new String[c.getCount()];
		if (c.moveToFirst())
        {
            do {          
            	data[counter]=c.getString(0);
            	counter++;
            } while (c.moveToNext());
        }
		return data;	
	}
	
	public String[][] readConf()
	{
		String[][] data=null;
		int counter=0;
		Cursor c = myDb.rawQuery("SELECT * FROM config"
				, null);
		this.rowCount=c.getCount();
        data= new String[c.getCount()][6];
		if (c.moveToFirst())
        {
            do {             	
            	data[counter][0]=c.getString(1);
            	data[counter][1]=c.getString(2);            	            	
            	counter++;
            } while (c.moveToNext());
        }
		return data;		
	}
	
	public String[] readStationName()
	{
		String[] data=null;
		int counter=0;
		Cursor c = myDb.rawQuery("SELECT name FROM station"
				, null);
        data= new String[c.getCount()];
		if (c.moveToFirst())
        {
            do {          
            	data[counter]=c.getString(0);
            	counter++;
            } while (c.moveToNext());
        }
		return data;	
	}
	
	
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	
}  
